package com.devicemantra.coordinate.ds.tests;
import static java.util.logging.Level.WARNING;
import  com.devicemantra.coordinate.dataobjects.CStateFactory;

import com.devicemantra.coordinate.dataobjects.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.jdo.JDOException;
import javax.jdo.JDOObjectNotFoundException;
import javax.jdo.PersistenceManager;
import javax.servlet.ServletException;
import javax.servlet.http.*;

import com.devicemantra.coordinate.dataobjects.CDepartmentFactory;
import com.devicemantra.coordinate.interfaces.call.CCallMessage;
import com.devicemantra.coordinate.interfaces.email.CSendEmail;
import com.devicemantra.coordinate.tasks.CTask;
import com.devicemantra.coordinate.tasks.CTaskFactory;
import com.devicemantra.coordinate.ds.tests.PMF;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

@SuppressWarnings("serial")
public class AuthGetStatus extends HttpServlet 
{ 
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException 
	{
		//final  Logger log = Logger.getAnonymousLogger();
		boolean lDoesExist = false;
		String lKey = req.getParameter("id");
		System.out.println("CALLED: /AuthGetStatus: id : " + lKey);
		//log.log(Level.ALL,"AuthGetStatus started ..");
		CAuthMobileMessage lAuthMessageObj = null;
		//Key lNewKey = KeyFactory.createKey(CAuthMobileMessage.class.getSimpleName(), lMessageId);
		
		
		/*PersistenceManager pm = PMF.get().getPersistenceManager();
		try
		{
			if(null != pm )
				lAuthMessageObj = pm.getObjectById(CAuthMobileMessage.class,lNewKey);
			
			
		} catch(JDOObjectNotFoundException e)
		{
			lDoesExist = false;
			resp.getOutputStream().println("Failure : Does not exist");
		}catch(JDOException e) {
			e.printStackTrace();
			resp.getOutputStream().println("Failure : to retreive object");
		} */
		
		PersistenceManager pm = PMF.get().getPersistenceManager();
		pm.getFetchPlan().setMaxFetchDepth(3);
	    List<CAuthMobileMessage> lMsgList = null;
	    javax.jdo.Query query = pm.newQuery(CAuthMobileMessage.class);
		query.setFilter("mMessageKey == lKey");
		query.declareParameters("String lKey");
	    lMsgList = (List<CAuthMobileMessage>) query.execute(lKey);
	    lMsgList = (List<CAuthMobileMessage>)pm.detachCopyAll(lMsgList);
	    System.out.println("Number of messages found  for " + lMsgList.size());
		pm.close();
		//createNewState();
		
		if(lMsgList.size() > 0) {
			//log.log(Level.ALL,"Fetching message size of list is " + lMsgList.size());
			System.out.println("Fetching message size of list is " + lMsgList.size());
			lAuthMessageObj = lMsgList.get(0);
		}
		else {
			System.out.println("Could not find object");
		}
		
		if(lAuthMessageObj != null) {
			System.out.println("Stauts of  " + lAuthMessageObj.getAuthMobileNumber() + " is "  +  lAuthMessageObj.getStatus());
			resp.getOutputStream().println(lAuthMessageObj.getStatus());
		}else {
			System.out.println("lAuthMessageObj was null  ");
		}
		System.out.println("closing PMF now !");
		
		 
		
	}
	
	public void doPost(HttpServletRequest req, HttpServletResponse resp)
	throws IOException  {
		doGet(req,resp);
	}
}

